package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.project.ProjectReceive;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ProjectReceiveService;
import com.xpsoft.oa.service.project.ProjectService;

public class ProjectReceiveFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(ProjectReceiveFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String proId = (String)pi.getVariable("projectReceive.project.id");
		String constructionUnit = (String)pi.getVariable("projectReceive.constructionUnit");
		String workUnit = (String)pi.getVariable("projectReceive.workUnit");
		String taxLocation = (String)pi.getVariable("projectReceive.taxLocation");
		String contact = (String)pi.getVariable("projectReceive.contact");
		Date beginSignDate = (Date)pi.getVariable("projectReceive.beginSignDate");
		Date endSignDate = (Date)pi.getVariable("projectReceive.endSignDate");
		BigDecimal actualTax = (BigDecimal)pi.getVariable("projectReceive.actualTax");
		String dataList = (String)pi.getVariable("projectReceive.dataList");
		
		ProjectReceive projectReceive = new ProjectReceive();
		projectReceive.setConstructionUnit(constructionUnit);
		projectReceive.setWorkUnit(workUnit);
		projectReceive.setTaxLocation(taxLocation);
		projectReceive.setContact(contact);
		projectReceive.setBeginSignDate(beginSignDate);
		projectReceive.setEndSignDate(endSignDate);
		projectReceive.setActualTax(actualTax);
		
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			projectReceive.setProject(projectService.get(Long.valueOf(proId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		projectReceive.setProcessRunId(processRun.getRunId());
		
		ProjectReceiveService projectReceiveService = (ProjectReceiveService)AppUtil.getBean("projectReceiveService");
		projectReceiveService.saveProjectReceive(projectReceive, dataList);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
